import React from 'react';
import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	Tools,
	Bubble,
	Toast,
	Icon,
	Avatar
} from "weaver-mobile";
import * as api from '../../apis/accountSetting';
const getLabel = Tools.getLabel;

class AccountSettingStore {
	@observable checkedShowType = 0;
	@observable userid = '';
	@observable accountlist = [];
	@observable showTypes = [];
	@observable accountType = '0';//主账号
	@observable primaryAccountId = '';
	@observable saveBtnDisabled = false;

	init = () => {
		this.loadingToast();
		this.saveBtnDisabled = true;
		this.getPageInfo().then(() => {
			this.hideToast();
			this.saveBtnDisabled = false;
		});
	}
	
	@action onShowTypeChange = (val) => {
		this.checkedShowType = val;
	}

	@action getPageInfo = () => {
		//获取多账号数据显示数据
		const p1 = api.getShowTypes();
		//获取账户信息
		const p2 = api.getAccount();
		return Promise.all([p1,p2]).then(datas => {
			datas.forEach((item, index) => {
				switch (index) {
					case 0:
						const {
							datas,
							belongtoshow,
							AccountType,
							belongtoId,
						} = item;
						this.showTypes = datas;
						this.checkedShowType = belongtoshow;
						this.accountType = AccountType;
						this.primaryAccountId = belongtoId;
					break;
					case 1:
						const {
							data
						} = item, {
							accountlist,
							userid
						} = data;

						this.accountlist = accountlist;
						this.userid = userid;
						break;
				}
			})
			
		}).catch(error => Bubble.error(error))
	}

	loadingToast = () => {
		Toast.loading('', 0);
	}

	hideToast = () => {
		Toast.hide()
	}

	//设置选中账户的ID
	set checkedAccount(val){
		this.userid = val;
	}

	@computed get checkedAccount(){
		return this.userid;
	}

	//根据accountlist衍生出组件所需要的数据
	@computed get accountInfo(){
		const datas = [];
		this.accountlist.forEach((list, index) => {
			const {
				subcompanyname,
				deptname,
				icon,
				username,
				jobs,
				userid,
			} = list;
			const labelCom = (
				<div>
					<Avatar userId={userid}/>
					<div className='right'>
						<div>
							{ (parseInt(userid) === parseInt(this.primaryAccountId) ) && <Icon type='zhuzhanghao' size='xxs' color='#FF9323'/> }
							<span>{username}</span>
							<span className='job'>{jobs}</span>
						</div>
						<div>
							<span>{`${subcompanyname} / ${deptname}`}</span>
						</div>
					</div>
				</div>
			)
			datas.push({
				value: userid,
				label: labelCom
			});
		})
		return datas;
	}

	@computed get showTypeDisabled(){
		//不为主账号时，不能设置【多账号数据显示】
		return this.accountType !== '0';
	}

	save = () => {
		this.saveBtnDisabled = true;

		this.saveShowType().then(() => {
			this.injectUserInfoToSession().then(() => {
				this.saveAccountInfo();
			})
		})
	}

	//保存多账号数据显示
	saveShowType = () => {
		return new Promise((resolve, reject) => {
			const params = {
				belongtoshow: this.checkedShowType
			};
			api.saveShowType(params).then(datas => {
				const {
					status,
					message
				} = datas;

				if (status === '1') {
					resolve();
				} else {
					Bubble.error(message);
					this.saveBtnDisabled = false;
					reject();
				}
			})
		})
	}

	//将用户信息写入session
	injectUserInfoToSession = () => {
		return new Promise((resolve, reject) => {
			const params = {
				shiftid: this.userid
			}
			api.injectUserInfoToSession(params).then(datas => {
				const {
					status
				} = datas;

				if (status === '1') {
					resolve();
				}else{
					reject();
					this.saveBtnDisabled = false;
				}
			});
		})
	}

	//切换主次账号
	saveAccountInfo=()=>{
		if (window.em && window.em.switchAccount) {
			window.em.switchAccount({
				sysId:window.localStorage.emobile_ec_id,
				accountId:this.userid,
				success:function(){
					Bubble.success(getLabel(83551, '保存成功！'));
					this.saveBtnDisabled = false;
				},
				fail:err=>{
					Bubble.error(JSON.stringify(err) );
					this.saveBtnDisabled = false;
				}
			})
		}else{
			this.saveBtnDisabled = false;
		}
	}

}

export const accountSettingStore = new AccountSettingStore();